using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Linq;

namespace DT.Art.VFX.DTEditorGUI
{
    using pKey = DTPropKeyName;
    using fkey = DTFilteKeyName;
    public class DTFullModuleGUI : BaseShaderGUI
    {
        protected override GUIContent[] uvmodeArray => new GUIContent[]
        {
            DTGUIEx.DTGetContent("Animation Value"),
            DTGUIEx.DTGetContent("Auto Speed"),
            DTGUIEx.DTGetContent("Particle Custom1.X"),
            DTGUIEx.DTGetContent("Particle Custom1.Y"),
            DTGUIEx.DTGetContent("Particle Custom1.Z"),
            DTGUIEx.DTGetContent("Particle Custom1.W")
        };
        // protected override string HanDistortion => "Distort Mode";
        protected override string HelpInfo => @"
  1. 1111
  2. 2222
  3. 3333";

        protected override void ModuleInit()
        {
            base.ModuleInit();

            // m_Module[ModuleType.ColorTex].DrawGUI = DrawColorTex;
            // m_Module[ModuleType.MaskMode].DrawGUI = DrawMaskTex;
            m_Module[ModuleKey.DistortTex].DrawGUI = DrawDistort;
            m_Module[ModuleKey.DissolveTex].DrawGUI = DrawDissolve;
            m_Module[ModuleKey.FNLMode].DrawGUI = DrawFNL;
            m_Module[ModuleKey.GroundClipMode].DrawGUI = DrawGroundClip;
            // m_Module[ModuleType.OutlineMode].DrawGUI = DrawOutline;
            // m_Module[ModuleType.SprintMode].DrawGUI = DrawSprint;

        }

        protected override void DrawPPModuleInit(MaterialProperty[] properties)
        {
            base.DrawPPModuleInit(properties);
            DTFindProperty("_FDistortEnabled", properties).floatValue = m_Module[ModuleKey.DistortTex].IsEnable ? 1 : 0;
            DTFindProperty("_FDissolveEnabled", properties).floatValue = m_Module[ModuleKey.DissolveTex].IsEnable ? 1 : 0;
            DTFindProperty("_FFnlEnabled", properties).floatValue = m_Module[ModuleKey.FNLMode].IsEnable ? 1 : 0;
            DTFindProperty("_FGroundEnabled", properties).floatValue = m_Module[ModuleKey.GroundClipMode].IsEnable ? 1 : 0;
        }

        // protected void DrawColorTex(DT_HeaderScope header, MaterialEditor materialEditor, MaterialProperty[] properties)
        // {

        // }

        // protected void DrawMaskTex(DT_HeaderScope header, MaterialEditor materialEditor, MaterialProperty[] properties)
        // {

        // }

        protected void DrawDistort(DT_HeaderScope header, MaterialEditor materialEditor, MaterialProperty[] properties)
        {
            DTGUIBitDate GUIDate = new DTGUIBitDate("Distort", " Distort Tex (扰动纹理)");
            GUIDate.Propertykeys[pKey.enable] = null;
            GUIDate.Propertykeys[pKey.tex] = DTFindProperty("_DistortTex", properties);
            GUIDate.Propertykeys[pKey.texColor] = null;
            GUIDate.Propertykeys[pKey.texST] = DTFindProperty("_DistortTex", properties);
            GUIDate.Propertykeys[pKey.texST1] = null;
            GUIDate.Propertykeys[pKey.texST2] = null;

            GUIDate.Propertykeys[pKey.uspeed] = DTFindProperty("_DistortUValue", properties);
            GUIDate.Propertykeys[pKey.vspeed] = DTFindProperty("_DistortVValue", properties);
            GUIDate.Propertykeys[pKey.matrix0] = DTFindProperty("_DistortMatrix0", properties);
            GUIDate.Propertykeys[pKey.matrix1] = DTFindProperty("_DistortMatrix1", properties);

            GUIDate.Propertykeys[pKey.brightness] = null;// DTFindProperty("_DistortBrightness", properties);
            GUIDate.Propertykeys[pKey.alpha] = null;// DTFindProperty("_DissolveAlpha", properties);

            GUIDate.Propertykeys[pKey.ToolgetSw] = DTFindProperty("_DistortSW", properties);
            GUIDate.Propertykeys[pKey.filterValue4] = DTFindProperty("_DistortFilterValue4", properties); ;

            GUIDate.SetBitProp(fkey.Umode, pKey.ToolgetSw, 0, 4, ChannelMode.x);
            GUIDate.SetBitProp(fkey.Vmode, pKey.ToolgetSw, 4, 4, ChannelMode.x);

            GUIDate.SetBitProp(fkey.Screen, pKey.ToolgetSw, 0, 1, ChannelMode.y);
            GUIDate.SetBitProp(fkey.Polar, pKey.ToolgetSw, 1, 1, ChannelMode.y);
            GUIDate.SetBitProp(fkey.Cx, pKey.ToolgetSw, 2, 1, ChannelMode.y);
            GUIDate.SetBitProp(fkey.Cy, pKey.ToolgetSw, 3, 1, ChannelMode.y);
            GUIDate.SetBitProp(fkey.Fx, pKey.ToolgetSw, 4, 1, ChannelMode.y);
            GUIDate.SetBitProp(fkey.Fy, pKey.ToolgetSw, 5, 1, ChannelMode.y);
            // GUIDate.SetBitProp(fkey.Distort, pKey.ToolgetSw, 13, 1, ChannelMode.y); GUIDate.SetBitView(fkey.Distort, false);
            GUIDate.SetBitProp(fkey.Frame, pKey.ToolgetSw, 14, 1, ChannelMode.y);

            GUIDate.IsCAddFilter = GUIDate.Propertykeys[pKey.texColor] != null;
            //--------
            GUIDate.UVBTNS = new List<string>()
            {
                fkey.Screen,fkey.Polar,fkey.Cx,fkey.Cy,fkey.Fx,fkey.Fy,fkey.Frame
            };
            GUIDate.InitProperty();
            if (GUIDate.GetProp(pKey.tex) == null || GUIDate.GetProp(pKey.ToolgetSw) == null)
            {
                return;
            }
            GUIDate.MEBeginProperty();
            DrawTexUV(header, materialEditor, properties, GUIDate);
            GUIDate.IsCAddFilter = false;
            if (header.expanded && GUIDate.IsCAddFilter)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                EditorGUI.indentLevel++;
                EditorGUILayout.HelpBox("Main Tex (主纹理)", MessageType.None);
                DTGUIEx.DrawLine(2);
                EditorGUILayout.HelpBox("Main Tex (主纹理)", MessageType.None);
                EditorGUILayout.HelpBox("Main Tex (主纹理)", MessageType.None);

                EditorGUI.indentLevel--;
                EditorGUILayout.EndVertical();
                EditorGUI.indentLevel--;
            }
            GUIDate.MEEndProperty();
        }

        protected void DrawDissolve(DT_HeaderScope header, MaterialEditor materialEditor, MaterialProperty[] properties)
        {
            DTGUIBitDate GUIDate = new DTGUIBitDate("Dissolve", " Dissolve Tex (溶解纹理)");
            GUIDate.Propertykeys[pKey.enable] = null;
            GUIDate.Propertykeys[pKey.tex] = DTFindProperty("_DissolveTex", properties);
            GUIDate.Propertykeys[pKey.texColor] = null;
            GUIDate.Propertykeys[pKey.texST] = DTFindProperty("_DissolveTex", properties);
            GUIDate.Propertykeys[pKey.texST1] = null;
            GUIDate.Propertykeys[pKey.texST2] = null;
            GUIDate.Propertykeys[pKey.uspeed] = DTFindProperty("_DissolveUValue", properties);
            GUIDate.Propertykeys[pKey.vspeed] = DTFindProperty("_DissolveVValue", properties);
            GUIDate.Propertykeys[pKey.matrix0] = DTFindProperty("_DissolveMatrix0", properties);
            GUIDate.Propertykeys[pKey.matrix1] = DTFindProperty("_DissolveMatrix1", properties);
            GUIDate.Propertykeys[pKey.ToolgetSw] = DTFindProperty("_DissolveSW", properties);
            GUIDate.Propertykeys[pKey.brightness] = null;// DTFindProperty("_DissolveBrightness", properties);
            GUIDate.Propertykeys[pKey.alpha] = null;// DTFindProperty("_DissolveAlpha", properties);

            GUIDate.Propertykeys[pKey.filterValue4] = DTFindProperty("_DissolveFilterValue4", properties);

            GUIDate.SetBitProp(fkey.Umode, pKey.ToolgetSw, 0, 4, ChannelMode.x);
            GUIDate.SetBitProp(fkey.Vmode, pKey.ToolgetSw, 4, 4, ChannelMode.x);

            GUIDate.SetBitProp(fkey.Screen, pKey.ToolgetSw, 0, 1, ChannelMode.y);
            GUIDate.SetBitProp(fkey.Polar, pKey.ToolgetSw, 1, 1, ChannelMode.y);
            GUIDate.SetBitProp(fkey.Cx, pKey.ToolgetSw, 2, 1, ChannelMode.y);
            GUIDate.SetBitProp(fkey.Cy, pKey.ToolgetSw, 3, 1, ChannelMode.y);
            GUIDate.SetBitProp(fkey.Fx, pKey.ToolgetSw, 4, 1, ChannelMode.y);
            GUIDate.SetBitProp(fkey.Fy, pKey.ToolgetSw, 5, 1, ChannelMode.y);

            GUIDate.SetBitProp(fkey.Distort, pKey.ToolgetSw, 13, 1, ChannelMode.y);
            GUIDate.SetBitProp(fkey.Frame, pKey.ToolgetSw, 14, 1, ChannelMode.y);

            const string DissolveLineColor = "_DissolveLineColor";
            const string DissolveDateValue = "_DissolveDateValue";
            const string DissolveFactor = "_DissolveFactor";
            GUIDate.Propertykeys.Add(DissolveLineColor, DTFindProperty(DissolveLineColor, properties));
            GUIDate.Propertykeys.Add(DissolveDateValue, DTFindProperty(DissolveDateValue, properties));
            GUIDate.Propertykeys.Add(DissolveFactor, DTFindProperty(DissolveFactor, properties));


            GUIDate.IsCAddFilter = GUIDate.Propertykeys[pKey.texColor] != null;
            //--------
            GUIDate.UVBTNS = new List<string>()
            {
                fkey.Screen,fkey.Polar,fkey.Cx,fkey.Cy,fkey.Fx,fkey.Fy,fkey.Distort,fkey.Frame
            };
            GUIDate.InitProperty();
            if (GUIDate.GetProp(pKey.tex) == null || GUIDate.GetProp(pKey.ToolgetSw) == null)
            {
                return;
            }
            GUIDate.MEBeginProperty();
            DrawTexUV(header, materialEditor, properties, GUIDate);
            GUIDate.IsCAddFilter = true;
            if (header.expanded && GUIDate.IsCAddFilter)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                // EditorGUI.indentLevel++;
                // _DissolveLineColor _DissolveDateValue _DissolveFactor 
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.PrefixLabel("Btn's");
                DTchannelDate modetmp = GUIDate.GetProp(DissolveDateValue).GetValue();
                int mode = (int)modetmp.w & 0x7;
                EditorGUILayout.Space(0, true);
                {
                    string[] modepopupkey = new string[] { "Anim Value", "Particle.a", "Custom1.x", "Custom1.y", "Custom1.z", "Custom1.w" };
                    DTGUIEx.DrawPopupField(materialEditor, GUIDate.GetProp(DissolveDateValue), GUIContent.none, modepopupkey, ChannelMode.w, 0, 4, width: 100, isEd: true);

                    DTGUIEx.DrawLastFieldLable(GUIDate.GetProp(DissolveDateValue), DTGUIEx.DTGetContent($"{modepopupkey[mode]}", "溶解线的颜色来源"));
                }
                DTGUIEx.ToggleBTNShaderProperty(materialEditor, GUIDate.GetProp(DissolveDateValue), DTGUIEx.DTGetContent("扰", "溶解线扰动"), ChannelMode.w, 5, 1);
                DTGUIEx.ToggleBTNShaderProperty(materialEditor, GUIDate.GetProp(DissolveDateValue), DTGUIEx.DTGetContent("模", "2种边缘线不同算法的模式,开启为作者习惯的细调模式"), ChannelMode.w, 4, 1);
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.Space(3, false);
                if (mode < 1)
                    DTGUIEx.DrawRangeField(materialEditor, GUIDate.GetProp(DissolveFactor), DTGUIEx.DTGetContent("Anim Value", "溶解的时间"), 0f, 1f);
                DTGUIEx.DrawRangeField(materialEditor, GUIDate.GetProp(DissolveDateValue), DTGUIEx.DTGetContent("Soft", "柔化边缘"), 0f, 1f, ChannelMode.x);
                DTGUIEx.DrawRangeField(materialEditor, GUIDate.GetProp(DissolveDateValue), DTGUIEx.DTGetContent("Line Wide", "边缘线宽度"), 0f, 1f, ChannelMode.y);
                DTGUIEx.DrawColorField(materialEditor, GUIDate.GetProp(DissolveLineColor), DTGUIEx.DTGetContent("Line Color", "溶解线的颜色"), hdr: true);
                DTGUIEx.DrawRangeField(materialEditor, GUIDate.GetProp(DissolveDateValue), DTGUIEx.DTGetContent("Line Dist", "溶解线扰动"), 0f, 1f, ChannelMode.z);

                // EditorGUI.indentLevel--;
                EditorGUILayout.EndVertical();
                EditorGUI.indentLevel--;
            }
            GUIDate.MEEndProperty();
        }
        protected void DrawFNL(DT_HeaderScope header, MaterialEditor materialEditor, MaterialProperty[] properties)
        {
            if (header.expanded)
            {
                MaterialProperty fnl = DTFindProperty("_FnlValue", properties);
                MaterialProperty fnlColor = DTFindProperty("_FnlColor", properties);
                if (fnl == null || fnlColor == null) return;
                string[] blendmode = new string[2] { "Add", "Mul" };//, "Lerp" };
                MaterialEditor.BeginProperty(fnl);
                MaterialEditor.BeginProperty(fnlColor);
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                EditorGUI.indentLevel++;
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("FNL Mode");
                EditorGUILayout.Space(0, true);
                bool isCustom = ((int)fnl.vectorValue.w >> 14 & 0x1) > 0;
                if (isCustom)
                {
                    //当前锁定FNL的颜色混合模式
                    DTGUIEx.DrawPopupField(materialEditor, fnl, GUIContent.none, blendmode, ChannelMode.w, 0, 4, width: 60, isEd: true);
                    int isblend = (int)fnl.vectorValue.w & 0x7;
                    EditorGUI.LabelField(GUILayoutUtility.GetLastRect(), DTGUIEx.DTGetContent($"{blendmode[isblend]}", "FNL的颜色与模型结果的混合方式。默认:ADD"));//, new GUIStyle(EditorStyles.label) { alignment = TextAnchor.MiddleCenter });
                }
                else
                {
                    Vector4 v = fnl.vectorValue;
                    v.w = DTUtil.ReplaceBits(0, (int)v.w, 0, 4);
                    fnl.vectorValue = v;
                }
                DTGUIEx.ToggleBTNShaderProperty(materialEditor, fnl, DTGUIEx.DTGetContent("A", "FNL参与透明通道的计算"), ChannelMode.w, 4, 1);
                DTGUIEx.ToggleBTNShaderProperty(materialEditor, fnl, DTGUIEx.DTGetContent("反", "翻转FNL的结果"), ChannelMode.w, 5, 1);
                DTGUIEx.ToggleBTNShaderProperty(materialEditor, fnl, DTGUIEx.DTGetContent("溶", "FNL的结果是否会被溶解"), ChannelMode.w, 6, 1);
                DTGUIEx.ToggleBTNShaderProperty(materialEditor, fnl, DTGUIEx.DTGetContent("扰", "FNL的结果是否会被扰动"), ChannelMode.w, 7, 1);

                string playMode = isCustom ? "自" : "简";
                DTGUIEx.ToggleBTNShaderProperty(materialEditor, fnl, DTGUIEx.DTGetContent(playMode, "FNL的调整模式,简单和自定义模式。区别在于对效果的可调整程度。"), ChannelMode.w, 14, 1);

                EditorGUILayout.Space(3, false);
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.Space(3, false);
                DTGUIEx.DrawRangeField(materialEditor, fnl, DTGUIEx.DTGetContent("Scale"), 0f, 2f, channleIndex: ChannelMode.x);
                DTGUIEx.DrawRangeField(materialEditor, fnl, DTGUIEx.DTGetContent("Soft"), 0f, 1f, channleIndex: ChannelMode.y);
                DTGUIEx.DrawColorField(materialEditor, fnlColor, DTGUIEx.DTGetContent("Color"), hdr: true);
                DTGUIEx.DrawFloatField(materialEditor, fnl, DTGUIEx.DTGetContent("Color Intensity"), channleIndex: ChannelMode.z);


                EditorGUI.indentLevel--;
                EditorGUILayout.EndVertical();
                MaterialEditor.EndProperty();
                MaterialEditor.EndProperty();
            }
        }
        protected void DrawOutline(DT_HeaderScope header, MaterialEditor materialEditor, MaterialProperty[] properties)
        {
            if (header.expanded)
            {
                EditorGUILayout.LabelField("Outline Model");
            }
        }
        protected void DrawGroundClip(DT_HeaderScope header, MaterialEditor materialEditor, MaterialProperty[] properties)
        {
            if (header.expanded)
            {
                // _FGroundEnabled _GroundClipSoft,_GroundClipHight;  
                MaterialProperty soft = DTFindProperty("_GroundClipSoft", properties);
                MaterialProperty hight = DTFindProperty("_GroundClipHight", properties);
                if (soft == null || hight == null) return;
                MaterialEditor.BeginProperty(soft);
                MaterialEditor.BeginProperty(hight);

                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                EditorGUI.indentLevel++;
                DTGUIEx.DrawFloatField(materialEditor, hight, DTGUIEx.DTGetContent("Hight Offset"));
                DTGUIEx.DrawRangeField(materialEditor, soft, DTGUIEx.DTGetContent("Soft"));

                EditorGUI.indentLevel--;
                EditorGUILayout.EndVertical();
                MaterialEditor.EndProperty();
                MaterialEditor.EndProperty();
            }
        }
    }
}
